module page {
    export class SavePage extends egret.Sprite {

        private bitmap: egret.Bitmap;

        private imgUrl: string;

        private saveBtn: ui.PushButton;

        private closeBtn: ui.PushButton;

        private container: egret.DisplayObjectContainer;

        public constructor(imgUrl: string) {
            super();
            this.drawBg();
            this.imgUrl = imgUrl;

            this.container = new egret.DisplayObjectContainer();
            this.touchEnabled = true;
            this.addEventListener(egret.TouchEvent.TOUCH_TAP, function(eve){
                eve.stopImmediatePropagation(); 
            }, this);
            this.addChild(this.container);

            this.bitmap = new egret.Bitmap();
            this.container.addChild(this.bitmap);

            this.saveBtn = new ui.PushButton(manager.ResManager.getBitmap("btn_save"));
            this.container.addChild(this.saveBtn);

            this.closeBtn = new ui.PushButton(manager.ResManager.getBitmap("btn_close_dialog"));
            this.container.addChild(this.closeBtn);

            this.showBtn(false);
            this.loadSaveImg();
        }

        private showBtn(isVisible:boolean):void {
            this.closeBtn.visible = isVisible;
            this.saveBtn.visible = isVisible;
        }

        private drawBg(): void {
            this.graphics.beginFill(0x000000, 0.8);
            this.graphics.drawRect(0, 0, manager.DisplayManager.STAGE_WIDTH, manager.DisplayManager.STAGE_HEIGHT);
            this.graphics.endFill();
        }

        private loadSaveImg(): void {
            let that = this;
            let bgImgUrl: string = this.imgUrl;
            let imgLoader = new egret.ImageLoader();
            imgLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                let imageLoader = <egret.ImageLoader>event.currentTarget;
                that.updateTexture(imageLoader.data);
            }, this);
            imgLoader.load(bgImgUrl);
        }

        private distance = 100;

        private updateTexture(bitmapData: egret.BitmapData): void {
            let texture = new egret.Texture();
            texture.bitmapData = bitmapData;
            this.bitmap.texture = texture;

            this.saveBtn.x = -this.saveBtn.width / 2;
            this.saveBtn.y = this.bitmap.height + this.distance;
            this.saveBtn.addEventListener(ui.PushButton.CLICK, this.onSave, this);

            this.bitmap.x = -this.bitmap.width / 2;

            this.closeBtn.x = this.bitmap.x + this.bitmap.width - this.closeBtn.width / 2;
            this.closeBtn.y = -this.closeBtn.height / 2;
            this.closeBtn.addEventListener(ui.PushButton.CLICK, this.onHide, this);


            this.container.x = manager.DisplayManager.STAGE_WIDTH / 2;
            this.container.y = (manager.DisplayManager.GAME_HEIGHT2
                - this.container.height * manager.DisplayManager.SCREEN_SCALE) / 2;

            this.container.scaleX = this.container.scaleY = manager.DisplayManager.SCREEN_SCALE;

            this.showBtn(true);
        }

        private onHide(eve): void {
            manager.DisplayManager.remove(this);
        }

        private onSave(eve): void {
            eve.stopImmediatePropagation(); 
            let path = manager.RandomManager.getRand() + ".png";
            console.log(this.imgUrl);
            wx.downloadFile({
                url: this.imgUrl,
                header: {},
                filePath: "",
                success: function (res) {
                    console.log("图片下载成功", res);
                    let path = res.tempFilePath;
                    wx.saveImageToPhotosAlbum({
                        filePath: path,
                        success: function (res) {
                        },
                        fail: function (res) {
                            console.log(res)
                        },
                        complete: function (res) {
                            console.log(res)
                        }
                    })
                },
                fail: function (res) {
                    console.log("失败", res)
                },
                complete: function (res) {
                    console.log("下载图片结束》》》》》", res);
                }
            })
        }

    }
}